package com.haoyang.newairedu;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.jams.goodox.utils.ToastUtil;
import com.tencent.mm.opensdk.modelbase.BaseResp;

/**
 * Created by DWCloud on 2016/9/9.
 */

public class WXResultBroadcast extends BroadcastReceiver {

    public static final String WXRESULT_ACTION = "WXRESULT_ACTION";

    WXResultListener wxResultListener;

    public WXResultListener getWxResultListener() {
        return wxResultListener;
    }

    public void setWxResultListener(WXResultListener wxResultListener) {
        this.wxResultListener = wxResultListener;
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        if (intent != null){
            int result = intent.getIntExtra("result",1);
            switch (result) {
                case BaseResp.ErrCode.ERR_OK:
                    ToastUtil.show(context, "支付成功");
                    if (wxResultListener != null){
                        wxResultListener.onSucceed();
                    }
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    if (wxResultListener != null){
                        wxResultListener.cancel();
                    }
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    ToastUtil.show(context, "支付失败");
                    if (wxResultListener != null){
                        wxResultListener.onFail();
                    }

                    break;
                default:
                    ToastUtil.show(context, "支付失败");
                    if (wxResultListener != null){
                        wxResultListener.onFail();
                    }
                    break;
            }
        }

    }

    public interface WXResultListener{
        void onSucceed();
        void cancel();
        void onFail();
    }

}
